package com.wilson.integration.lease.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilson.integration.lease.app.service.ApartmentInfoService;
import com.wilson.integration.lease.app.service.BrowsingHistoryService;
import com.wilson.integration.lease.common.constant.RedisConstant;
import com.wilson.integration.lease.common.entity.*;
import com.wilson.integration.lease.common.entity.vo.apartment.ApartmentItemVO;
import com.wilson.integration.lease.common.entity.vo.attr.AttrValueVO;
import com.wilson.integration.lease.common.entity.vo.fee.FeeValueVO;
import com.wilson.integration.lease.common.entity.vo.graph.GraphVO;
import com.wilson.integration.lease.common.entity.vo.room.RoomDetailVO;
import com.wilson.integration.lease.common.entity.vo.room.RoomItemVO;
import com.wilson.integration.lease.common.entity.vo.room.RoomQueryVO;
import com.wilson.integration.lease.common.enums.ItemType;
import com.wilson.integration.lease.common.login.LoginUserHolder;
import com.wilson.integration.lease.common.mapper.*;
import com.wilson.integration.lease.app.service.RoomInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wilson
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo> implements RoomInfoService {

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;

    @Resource
    private LeaseTermMapper leaseTermMapper;

    @Resource
    private FacilityInfoMapper facilityInfoMapper;

    @Resource
    private LabelInfoMapper labelInfoMapper;

    @Resource
    private PaymentTypeMapper paymentTypeMapper;

    @Resource
    private AttrValueMapper attrValueMapper;

    @Resource
    private FeeValueMapper feeValueMapper;

    @Resource
    private ApartmentInfoService apartmentInfoService;

    @Resource
    private BrowsingHistoryService browsingHistoryService;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public IPage<RoomItemVO> pageAppRoomItemByQuery(Page<RoomItemVO> page, RoomQueryVO queryVO) {
        return roomInfoMapper.pageAppRoomItemByQuery(page, queryVO);
    }

    @Override
    public RoomDetailVO getDetailById(Long id) {
        RoomDetailVO roomDetailVO =  null;
        String key = RedisConstant.APP_ROOM_KEY + id;
        Object redisObject = redisTemplate.opsForValue().get(key);
        if (ObjectUtil.isNotNull(redisObject)) {
            roomDetailVO = JSON.parseObject(redisObject.toString(), RoomDetailVO.class);
        } else {
            // 1.查询房间信息
            RoomInfo roomInfo = roomInfoMapper.selectById(id);
            if (ObjectUtil.isNull(roomInfo)) {
                return null;
            }
            // 2.查询图片
            List<GraphVO> graphVOList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
            // 3.查询租期
            List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
            // 4.查询配套
            List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
            // 5.查询标签
            List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);
            // 6.查询支付方式
            List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
            // 7.查询基本属性
            List<AttrValueVO> attrValueVoList = attrValueMapper.selectListByRoomId(id);
            // 8.查询杂费信息
            List<FeeValueVO> feeValueVoList = feeValueMapper.selectListByApartmentId(roomInfo.getApartmentId());
            // 9.查询公寓信息
            ApartmentItemVO apartmentItemVo = apartmentInfoService.selectApartmentItemVOById(roomInfo.getApartmentId());

            roomDetailVO = new RoomDetailVO();
            BeanUtils.copyProperties(roomInfo, roomDetailVO);

            roomDetailVO.setApartmentItemVO(apartmentItemVo);
            roomDetailVO.setGraphVOList(graphVOList);
            roomDetailVO.setAttrValueVOList(attrValueVoList);
            roomDetailVO.setFacilityInfoList(facilityInfoList);
            roomDetailVO.setLabelInfoList(labelInfoList);
            roomDetailVO.setPaymentTypeList(paymentTypeList);
            roomDetailVO.setFeeValueVOList(feeValueVoList);
            roomDetailVO.setLeaseTermList(leaseTermList);

            redisTemplate.opsForValue().set(key, JSONObject.toJSONString(roomDetailVO));
        }

        // 10.异步保存浏览记录
        browsingHistoryService.saveHistory(LoginUserHolder.getLoginUser().getUserId(), id);

        return roomDetailVO;
    }

    @Override
    public IPage<RoomItemVO> pageItemByApartmentId(IPage<RoomItemVO> page, Long id) {
        return roomInfoMapper.pageItemByApartmentId(page, id);
    }
}




